package com.test.nnooolwdeaer.util_wooden.imageUtil_wooden;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden.BaseApplication_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ImageConvert_wooden {

    public static final String TAG_wooden = "FileChecker";

    public static String getImageDir_wooden() {
        String imgDir = BaseApplication_wooden.getApplication().getApplicationContext().getFilesDir() + "/" + Constant_wooden.PRODUCT_wooden + "Pics";
        File dirFile = new File(imgDir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        return imgDir;
    }

    public static String getImageFilePath_wooden() {
        String pictureId = (System.currentTimeMillis() + "");
        String imgDir = getImageDir_wooden();
        return imgDir + "/" + pictureId + ".jpg";
    }


    public static Bitmap getBitmapFromBytes_wooden(byte[] bytes) {
        Bitmap bitmap = null;
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }

        return bitmap;
    }

    public static Bitmap getBitmapFromFilePath_wooden(String filePath) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

    public static File getBitmapFile_wooden(Bitmap bitmap) throws IOException {
        if (bitmap == null) {
            return null;
        }

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }

        String targetPath = ImageConvert_wooden.getImageFilePath_wooden();
        File outFile = new File(targetPath);

        FileOutputStream fos = new FileOutputStream(outFile);
        fos.write(stream.toByteArray());
        fos.flush();
        fos.close();
        stream.flush();
        stream.close();

        return outFile;
    }

    public static String getFilePathFromBitmap_wooden(Bitmap bitmap) throws IOException {
        if (bitmap == null) {
            return null;
        }

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }

        String targetPath = ImageConvert_wooden.getImageFilePath_wooden();
        File outFile = new File(targetPath);

        FileOutputStream fos = new FileOutputStream(outFile);
        fos.write(stream.toByteArray());
        fos.flush();
        fos.close();
        stream.flush();
        stream.close();

        return targetPath;
    }

    public static String getFileExtendSuffix_wooden(File file) {
        try {
            FileInputStream input = new FileInputStream(file);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(input, null, options);
            return options.outMimeType.replace("image/", ".");
        } catch (Exception e) {
            return ".jpg";
        }
    }

    public static boolean isExternalStorageDocument_wooden(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    public static boolean isDownloadsDocument_wooden(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    public static boolean isMediaDocument_wooden(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    public static boolean isGooglePhotosUri_wooden(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }

    public static void startConvertImage_wooden(File file, final OnImageConvertListener onOnImageConvertListener) {
        try {
            if (file != null) {
                String suffix = getFileExtendSuffix_wooden(file);
                String targetDir = ImageConvert_wooden.getImageDir_wooden();
                String targetPath = targetDir + "/" +
                        System.currentTimeMillis() +
                        (TextUtils.isEmpty(suffix) ? ".jpg" : suffix);

                File outFile = new File(targetPath);
                File result = ImageChecker_wooden.SINGLE_wooden.needCompress_wooden(300, file.getAbsolutePath()) ?
                        new ImageCompress_wooden(file, outFile, false).compress_wooden() : file;

                String localPath = result.getAbsolutePath();
                Bitmap thumpBitmap = getBitmapFromFilePath_wooden(localPath);

                if (onOnImageConvertListener != null) {
                    onOnImageConvertListener.onSuccess(localPath, thumpBitmap);
                }
            }
        } catch (IOException e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
    }

    public interface OnImageConvertListener {
        /**
         * 返回转换后的路径与图片
         */
        void onSuccess(String localPath, Bitmap thumpImg);
    }
}
